import os
import re


_LOG_START = r'\|-----\|--------------------------------------------------------\|--------------------------------------------------------\|'
_LOG_END = r'------------------------------------总结-----------------------------------------'
_FAILED_TEST = '请见以下不同点:'

_DIR_PATH = 'C:/Users/wangx/Desktop/code/panwei-test/'
_TEST_LOG_PATH = _DIR_PATH + 'test_log.txt'
_LOG_PATH = _DIR_PATH + 'nohup0301.out'

def main():
        
    with open(_TEST_LOG_PATH, encoding='utf-8') as f:
        lines = f.readlines()

    num_lines = len(lines)
    i = 0

    while i < num_lines:
        print(i)
        line = lines[i]
        if re.findall(_LOG_START, line):
            end_idx = lines[i - 2].rfind('.sql')
            testcase_name = 'test_' + lines[i - 2][:end_idx + 4].split('/')[-1]
            j = i + 1
            while j < num_lines:
                if re.findall(_LOG_END, lines[j]):
                   break;
                j += 1
            if j + 1 < num_lines:
                result = ['实际结果：']
                expected_result = ['期望结果：']
                with open(_DIR_PATH + testcase_name, 'w+', encoding='utf-8') as f:
                    for test_result in lines[i:j]:
                        test_result = test_result.rstrip('|')
                        blocks = test_result.split('|')
                        expected_result.append(blocks[-2])
                        result.append(blocks[-3])
                    f.writelines('\n'.join(result))
                    f.write('\n')
                    f.writelines('\n'.join(expected_result))
            i = j + 1
        else:
            i += 1


if __name__ == '__main__':
    main()